$(function() {
	$("#userLogin").click(loginPro);
	$("#register").click(registerPro);
	$("#manager_submit").click(managerLoginPro);
	$("#manager_register").click(managerRegisterPro);
	$("#addUser").click(addUserPro);
	$("#addGroupBtn").click(addGroup);
	$("#addHomeworkBtn").click(addHomework);
	$("#updateHomework").click(updateHomeworkPro);
	$(".uploadHomeworkBtn").click(uploadHomeworkPro);
	tableView();
	
});

function loginPro(){
	var name = $.trim($("#name").val());
	var password = $.trim($("#password").val());
	var phone = $.trim($("#phone").val());
	
	if (name==null || name==""
		|| password==null || password==""
		|| phone==null || phone=="") 
	{
		loginInfoBlankShow();
		setTimeout(loginInfoBlankHide,2000);
		
	}else{
		$.post("login",$('#name,#password,#phone').serializeArray(), 
			function(data){
				this;
				if (eval(data)==2) {
					loginInfoFalseShow();
					setTimeout(loginInfoFalseHide,2000);
				}else if(eval(data)==1){
					loginInfoRightShow();
					setTimeout(loginInfoRightHide,1000);
					window.location.href="index";
				}
			});
	}
}

function loginInfoBlankShow(){
	$("#loginInfoBlank").show().dialog(
			{
				modal:true,
				resizable:true,
				width:300,
				height:100
			}
	);
}

function loginInfoBlankHide(){
	$("#loginInfoBlank").dialog("close");
}
function loginInfoFalseShow(){
	$("#loginInfoFalse").show().dialog(
			{
				modal:true,
				resizable:true,
				width:300,
				height:100
			}
	);
}

function loginInfoFalseHide(){
	$("#loginInfoFalse").dialog("close");
}

function loginInfoRightShow(){
	$("#loginInfoRight").show().dialog(
			{
				modal:true,
				resizable:true,
				width:300,
				height:100
			}
	);
}

function loginInfoRightHide(){
	$("#loginInfoRight").dialog("close");
}

function registerPro(){
	var id = $.trim($("#name").val());
	var password = $.trim($("#password").val());
	var passworld2 = $.trim($("#password2").val());
	var phone = $.trim($("#phone").val());
	var email = $.trim($("#email").val());
	var address = $.trim($("#address").val());
	alert(img);
	if (id==null || id==""
		|| password==null || password==""
		|| password2 == null || password2 == ""
		|| password2 == null || password2 == ""
			|| phone == null || phone == ""
				|| email == null || email == ""
					|| address == null || address == "") 
	{
		registerInfoBlankShow();
		setTimeout(registerInfoBlankHide,2000);
	}else if(password != passworld2){
		registerInfoDiffPasswordShow();
		setTimeout(registerInfoDiffPasswordHide,2000);
	}else{
		$.post("register",$('#name,#password,#password2,#phone,#email,#address').serializeArray(), 
			function(data){
				this;
				if (eval(data)==1) {
					registerInfoUserExistShow();
					setTimeout(registerInfoUserExistHide,2000);
				}else if(eval(data)==2){
					registerInfoRightShow();
					setTimeout(registerInfoRightHide,1000);
					window.location.href="index";
				}else{
					registerInfoFalseShow();
					setTimeout(registerInfoFalseHide,2000);
				}
			});
	}
}
function registerInfoBlankShow(){
	$("#registerInfoBlank").show().dialog(
			{
				modal:true,
				resizable:true,
				width:300,
				height:100
			}
	);
}

function registerInfoBlankHide(){
	$("#registerInfoBlank").dialog("close");
}

function registerInfoDiffPasswordHide(){
	$("#registerInfoPasswordDiff").dialog("close");
}

function registerInfoDiffPasswordShow(){
	$("#registerInfoPasswordDiff").show().dialog(
			{
				modal:true,
				resizable:true,
				width:300,
				height:100
			}
	);
}

function registerInfoFalseShow(){
	$("#registerInfoFalse").show().dialog(
			{
				modal:true,
				resizable:true,
				width:300,
				height:100
			}
	);
}



function registerInfoFalseHide(){
	$("#registerInfoFalse").dialog("close");
}

function registerInfoRightShow(){
	$("#registerInfoRight").show().dialog(
			{
				modal:true,
				resizable:true,
				width:300,
				height:100
			}
	);
}

function registerInfoRightHide(){
	$("#registerInfoRight").dialog("close");
}

function registerInfoUserExistShow(){
	$("#registerInfoUserExist").show().dialog(
			{
				modal:true,
				resizable:true,
				width:300,
				height:100
			}
	);
}

function registerInfoUserExistHide(){
	$("#registerInfoUserExist").dialog("close");
}

function managerLoginPro(){
	var name = $.trim($("#name").val());
	var password = $.trim($("#password").val());
	var phone = $.trim($("#phone").val());
	
	if (name==null || name==""
		|| password==null || password==""
		|| phone==null || phone=="") 
	{
		loginInfoBlankShow();
		setTimeout(loginInfoBlankHide,2000);
		
	}else{
		$.post("managerLogin",$('#name,#password,#phone').serializeArray(), 
			function(data){
				this;
				if (eval(data)==2) {
					loginInfoFalseShow();
					setTimeout(loginInfoFalseHide,2000);
				}else if(eval(data)==1){
					loginInfoRightShow();
					setTimeout(loginInfoRightHide,1000);
					window.location.href="restaurant";
				}
			});
	}
}

function managerRegisterPro(){
	var name = $.trim($("#name").val());
	var password = $.trim($("#password").val());
	var passworld2 = $.trim($("#password2").val());
	var phone = $.trim($("#phone").val());
	var restaurantName =$.trim($("#restautantName").val()); 
	if (name==null || name==""
		|| password==null || password==""
		|| password2 == null || password2 == ""
		|| password2 == null || password2 == ""
			|| phone == null || phone == ""
				||restautantName == null || restautantName == "" )
	{
		registerInfoBlankShow();
		setTimeout(registerInfoBlankHide,2000);
	}else if(password != passworld2){
		registerInfoDiffPasswordShow();
		setTimeout(registerInfoDiffPasswordHide,2000);
	}else{
		$.post("managerRegister",$('#name,#password,#password2,#phone,#restaurantName').serializeArray(), 
			function(data){
				this;
				if (eval(data)==1) {
					registerInfoUserExistShow();
					setTimeout(registerInfoUserExistHide,2000);
				}else if(eval(data)==2){
					registerInfoRightShow();
					setTimeout(registerInfoRightHide,1000);
					window.location.href="restaurant";
				}else{
					registerInfoFalseShow();
					setTimeout(registerInfoFalseHide,2000);
				}
			});
	}
}

function addUserPro(){
	var id = $.trim($("#studentid").val());
	var name = $.trim($("#studentname").val());
	var password = $.trim($("#password").val());
	var password2 = $.trim($("#password2").val());
	
	if (id==null || id == "" ||id==0|| name==null || name=="" ||
		password==null||password==""||password2==null || password2=="") 
	{
		alert("ѧ�Ż����벻��Ϊ��");	
	}else if(password != password2){
		alert("�������벻һ��");
	}else {
		$.post("add",$('#studentid,#studentname,#password').serializeArray(),
				function(data) {
					this;
					switch(eval(data)){
						case 1:
							alert("���û��Ѵ���");
							break;
						case 2:
							alert("��ӳɹ�");
							window.location.href="index";
							break;
						case 0:
							alert("���ʧ��");
							break;	
					}
				}
		);
	}
	
}

function addGroup()
{
	var role=$("#showrole").val();
	if (role!="student") {
		alert("�����ݲ���ѧ���ܴ���С��");
		return;
	};
	$("#addGroupFiled").show().dialog(
		{
			modal:true,
			resizable:true,
			width:500,
			height:300
		}
	);
	$("#addGroup").click(addGroupPro);
	$("#cancelAddGroup").click(cancelAddGroupPro);
	
}
function addGroupPro(){
	var name=$.trim($("#groupname").val());
	var description = $.trim($("#description").val());
	
	if (name==null || name=="" || description==null || description=="") {
		alert("��Ϣ����Ϊ��");
	} else{
		$.post("createGroup",{name:name,description:description},
		function(data) {
			this;
			switch(eval(data)){
				case 1:
					alert("����ͬʱ��������С��");
					break;
				case 2:
					alert("��ӳɹ�");
					$("#addGroupFiled").hide().dialog("close");
					window.location.href="group";
					break;
				case 3:
					alert("���ʧ��");
					break;
			}
		});
	};
}

function cancelAddGroupPro(){
	$("#addGroupFiled").hide().dialog("close");
}

function addHomework(){
	$("#addHomeworkFiled").show().dialog(
		{
			modal:true,
			resizable:true,
			width:500,
			height:300
		}
	);
	$("#addHomework").click(addHomeworkPro);
	$("#cancelAddHomework").click(function(){
		$("#addHomeworkFiled").hide().dialog("close");
	});
}

function addHomeworkPro() {
	var title=$.trim($("#title").val());
	var content=$.trim($("#_content").val());
	var deadline=$.trim($("#deadline").val());
	
	if (title==null || title=="" 
		|| content==null || content == ""
		|| deadline == null || deadline=="")
	{
		alert("��Ϣ����Ϊ��");
	}
	else{
		$.post("homework",{title:title,content:content,deadline:deadline},
		function(data){
			this;
			switch(eval(data)){
				case 1:
					alert("�����ɹ�");
					$("#addHomeworkFiled").hide().dialog("close");
					window.location.href = "homework!showHomework.action";
					break;
				case 2:
					alert("����ʧ��");
					break;
			}
		});
	}
}




function updateHomeworkPro(){
	var id = $.trim($("#updateHomeworkId").val());
	var title=$.trim($("#updateHomeworkTitle").val());
	var content=$.trim($("#updateHomeworkContent").val());
	var deadline=$.trim($("#updateHomeworkDeadline").val());
	
	if (id == null || id==""
		|| title==null || title=="" 
		|| content==null || content == ""
		|| deadline == null || deadline=="")
	{
		alert("��Ϣ����Ϊ��");
	}else{
		$.post("homework",{id:id,title:title,content:content,deadline:deadline},
		function(data){
			this;
			switch(eval(data)){
				case 1:
					alert("�޸ĳɹ�");
					window.location.href="homework!showHomework.action";
					break;
				case 2:
					alert("�޸�ʧ��");
					break;
			}
		});
	}
}

function uploadHomeworkPro(){
		alert($(this).text());
}

function tableView() {
	$("table #tbody tr:odd").css("background-color","#D2D8DE");
	$("table #tbody tr:even").css("background-color","#FFFFFF");
}





